<?php
namespace App\Services\Fund\IntegralAccount;

use App\Models\Fund\IntegralAccount;
use App\Models\Fund\integralOptionLog;
use App\Models\Fund\IntegralFreezeLog;
use App\Models\Fund\OperateReason;
use App\Models\Fund\IntegralUnfreezeLog;
use App\Services\User\User;
use Illuminate\Support\Facades\DB;


class IntegralAccountLogic
{
    /*值为money的亚数币/余额 转换为多少积分  */
//    public function exchange(FundInterface $fundClass, $data): array
//    {
//        if($data['money'] == 0){
//            return auto_success(0);
//        }
//        $this->_updateScale($data);
//        if($data['exchange'] == 'Currency'){ //若用数字币转化为积分：数字币->人民币
//            $toFund['app_id'] = $data['app_id'];
//            $toFund['exchange'] = 'Currency';
//            $toFund['money'] = $data['money'];
//            $toFund['currency_scale'] = $data['currency_scale'];
//            $classPath = '\app\serverfund\logic' . '\\' . 'CurrencyAccount';
//            $Obj = new $classPath($toFund);
//            $toFundRes = logic('\app\serverfund\logic\FundAccount')->exchange($Obj, $toFund);
//            if($toFundRes['status'] != SUCCESS_STATUS){
//                return $toFundRes;
//            }
//            $data['money'] = $toFundRes['res']['autoRes'];
//        }
//        //$res = bcmul( bcdiv($fundClass->scale, $this->scale, DECIMALS), $data['money'] );  //人民币->积分
//        $res = bcdiv(bcmul($fundClass->scale, $data['money']), $this->scale, DECIMALS);  //人民币->积分
//        if($res == 0){ $res = 1; }
//        return auto_return($res);
//    }

//    //修改属性比例值
//    private function _updateScale($data)
//    {
//        $config = config('fundScale');
//        $this->scale = $config[$data['app_id']]['Integral'];
//    }
//
    // 补全用户积分日志记录
    private function _replenishIntegralLogInfo(array $save, \App\Models\Fund\IntegralAccount $info): array
    {
        $save['total'] = $info['total'];
        $save['income'] = $info['income'];
        $save['expend'] = $info['expend'];
        $save['available_integral'] = $info['available_integral'];
        $save['freeze'] = $info['freeze'];
        return $save;
    }

    // 补全用户积分日志信息并存到相应数据表中,返回ID
    private function _saveIntegralLogInfo(...$userLogs): int
    {
        $c=count($userLogs);
        for($i = 0; $i<$c; $i++){
            $userLogs[$i]['update_time'] = NOW;
            $userLogs[$i]['create_time'] = NOW;
        }
       // dd($userLogs);
        integralOptionLog::insert($userLogs);
        return DB::getPdo()->lastInsertId();
    }

//    // 初始化积分账户
//    public function init(array $data): array
//    {
//        $saveAccount['user_id'] = $data['user_id'];
//        $saveAccount['create_ip'] = $data['create_ip'];
//        $saveAccount['total'] = 0;
//        $saveAccount['income'] = 0;
//        $saveAccount['expend'] = 0;
//        $saveAccount['available_integral'] = 0;
//        $saveAccount['freeze'] = 0;
//        $saveAccount['update_ip'] = '';
//
//        $saveLog['user_id'] = $data['user_id'];
//        $saveLog['opposite_id'] = SYSTEM_ID;
//        $saveLog['app_id'] = $data['app_id'];
//        $saveLog['type'] = 'init';
//        $saveLog['integral'] = 0;
//        $saveLog['total'] = 0;
//        $saveLog['income'] = 0;
//        $saveLog['expend'] = 0;
//        $saveLog['available_integral'] = 0;
//        $saveLog['freeze'] = 0;
//        $saveLog['remark'] = '用户[' . $data['user_id'] . ']积分初始化';
//        $saveLog['create_ip'] = $data['create_ip'];
//        $saveLog['update_ip'] = '';
//        $saveLog['status'] = 1;
//        $saveLog['user_info'] = '{}';
//        $saveLog['opposite_info'] = '{}';
//
//        // 启动事务
//        $this->db()->startTrans();
//        try{
//            $resAccount = $this->model($this->integralAccountModel)->save($saveAccount);
//            $resLog = $this->model($this->integralOptionLogModel)->save($saveLog);
//            // 提交事务
//            $this->db()->commit();
//        } catch (\Exception $e) {
//            // 回滚事务
//            $this->db()->rollback();
//            return server_err(null, '数据库异常:' . $e->getMessage());
//        }
//
//        return auto_return($resAccount, '成功初始化积分账户');
//    }
    # 充值 系统->a
    public function prepaid(array $data): array
    {
        // 更改用户积分
        $whereUpdateUserAccount['user_id'] = $data['user_id'];
        // 更改系统积分
        $whereUpdateSystemAccount['user_id'] = SYSTEM_ID;
        // 用户积分日志(部分)
        $userInfo = json_decode($data['user_info'], true);
        $oppositeInfo = json_decode($data['opposite_info'],true);

        $userAction = isset($userInfo['name']) ? $userInfo['name'] : $data['user_id'];
        $remark = (isset($data['remark']) && $data['remark']) ? '为用户[' . $userAction . ']充值：充值原因：' . $data['remark'] : '为用户[' . $userAction . ']充值';
        $saveUserLog['user_id'] = $data['user_id'];
        $saveUserLog['opposite_id'] = SYSTEM_ID;
        $saveUserLog['app_id'] = $data['app_id'];
        $saveUserLog['type'] = 'prepaid';
        $saveUserLog['integral'] = $data['integral'];
        $saveUserLog['remark'] = $remark;
        $saveUserLog['create_ip'] = $data['update_ip'];
        $saveUserLog['update_ip'] = $data['update_ip'];
        $saveUserLog['status'] = 1;
        $saveUserLog['user_info'] = $data['user_info'];
        $saveUserLog['opposite_info'] = $data['opposite_info'];
        // 系统积分日志(部分)
        $saveSystemLog['user_id'] = SYSTEM_ID;
        $saveSystemLog['opposite_id'] = $data['user_id'];
        $saveSystemLog['app_id'] = $data['app_id'];
        $saveSystemLog['type'] = 'prepaid';
        $saveSystemLog['integral'] = -$data['integral'];
        $saveSystemLog['remark'] = $remark;
        $saveSystemLog['create_ip'] = $data['update_ip'];
        $saveSystemLog['update_ip'] = $data['update_ip'];
        $saveSystemLog['status'] = 1;
        $saveSystemLog['user_info'] = $data['user_info'];
        $saveSystemLog['opposite_info'] = $data['opposite_info'];


        // 启动事务
        DB::beginTransaction();
        try{
            // 更改用户积分  //注：使用save方法进行更新时，连续两次数据的更新只会成功一条，尽管更新条件和更新数据
            //$resUserAccount = $this->model($this->integralAccountModel)->save($updateUserAccount, $whereUpdateUserAccount);
            $resUserAccount = DB::update("update `tp_integral_account` set `update_ip` = ?, `total` = (total+?), `income` = (income+?), `available_integral` = (available_integral+?) where `user_id` = ?",[$data['update_ip'],$data['integral'],$data['integral'],$data['integral'],$data['user_id']]);
            // 更改系统积分
            //$resSystemAccount = $this->model($this->integralAccountModel)->save($updateSystemAccount, $whereUpdateSystemAccount);
            $resSystemAccount = DB::update("update `tp_integral_account` set `update_ip` = ?, `total` = (total-?), `expend` = (expend+?), `available_integral` = (available_integral-?) where `user_id` = ?",[$data['update_ip'],$data['integral'],$data['integral'],$data['integral'],SYSTEM_ID]);
            // 查询当前用户和系统的积分状态
            $infoUserAccount = IntegralAccount::SharedLock()->where($whereUpdateUserAccount)->first();
            // 查询当前系统的积分状态
            $infoSystemAccount = IntegralAccount::SharedLock()->where($whereUpdateSystemAccount)->first();
            if(is_null($infoUserAccount) || is_null($infoSystemAccount)){
                return verify_err(null, '当前用户或系统用户的积分账户不存在');
            }
            // 补全用户积分日志
            $saveUserLog = $this->_replenishIntegralLogInfo($saveUserLog, $infoUserAccount);
            // 补opposite积分日志
            $saveSystemLog = $this->_replenishIntegralLogInfo($saveSystemLog, $infoSystemAccount);
            // 写入积分日志
            $this->_saveIntegralLogInfo($saveSystemLog, $saveUserLog);
            // 提交事务
            DB::commit();
        } catch (\Exception $e) {
            // 回滚事务
            DB::rollback();
            return server_err(null, '数据库异常:' . $e->getMessage());
        }

        return auto_return($infoUserAccount, '充值成功');
    }
    /*积分充值 系统->a,b,c...*/
    public function prepaidByUids(array $data): array
    {
        //一个一个用户的去充值
        foreach ($data['user_infos'] as $k => $v) {
            $prepaidData['integral'] = $v['integral'];
            $prepaidData['update_ip'] = $data['update_ip'];
            $prepaidData['app_id'] = $data['app_id'];
            $prepaidData['opposite_info'] = json_encode($data['opposite_info']);
            $prepaidData['user_id'] = $v['id'];
            $prepaidData['user_info'] = json_encode(['id'=>$v['id']]);
            $prepaidRes = $this->prepaid($prepaidData);
            if($prepaidRes['status'] != SUCCESS_STATUS){
                return verify_err($prepaidRes, '给用户【' . $v['user_id'] . '】充值失败');
            }
        }
        return auto_success($data, '成功给指定的多个用户充值成功');
    }
//    # 转账 a->b
//    public function transfer(array $data): array
//    {
//        // 更改用户积分
//        $whereUpdateUserAccount['user_id'] = $data['user_id'];
//        $whereUpdateUserAccount['available_integral'] = ['EGT', $data['integral']];
//        $updateUserAccount['update_ip'] = $data['update_ip'];
//        $updateUserAccount['total'] = ['exp', 'total-' . $data['integral']];
//        $updateUserAccount['expend'] = ['exp', 'expend+' . $data['integral']];
//        $updateUserAccount['available_integral'] = ['exp', 'available_integral-' . $data['integral']];
//        // 更改对方积分
//        $whereUpdateOppositeAccount['user_id'] = $data['opposite_id'];
//        $updateOppositeAccount['update_ip'] = $data['update_ip'];
//        $updateOppositeAccount['total'] = ['exp', 'total+' . $data['integral']];
//        $updateOppositeAccount['income'] = ['exp', 'income+' . $data['integral']];
//        $updateOppositeAccount['available_integral'] = ['exp', 'available_integral+' . $data['integral']];
//        // 用户积分日志(部分)
//        $userInfo = json_decode($data['user_info'], true);
//        $oppositeInfo = json_decode($data['opposite_info'], true);
//        $saveUserLog['user_id'] = $data['user_id'];
//        $saveUserLog['opposite_id'] = $data['opposite_id'];
//        $saveUserLog['app_id'] = $data['app_id'];
//        $saveUserLog['type'] = 'transfer';
//        $saveUserLog['integral'] = -$data['integral'];
//        $saveUserLog['remark'] = isset($oppositeInfo['name']) ? '转账给用户[' . $oppositeInfo['name'] . ']积分' : '转账给用户[' . $data['opposite_id'] . ']积分';
//        $saveUserLog['create_ip'] = $data['update_ip'];
//        $saveUserLog['update_ip'] = $data['update_ip'];
//        $saveUserLog['status'] = 1;
//        $saveUserLog['user_info'] = $data['user_info'];
//        $saveUserLog['opposite_info'] = $data['opposite_info'];
//        // opposite用户积分日志(部分)
//        $saveOppositeLog['user_id'] = $data['opposite_id'];
//        $saveOppositeLog['opposite_id'] = $data['user_id'];
//        $saveOppositeLog['app_id'] = $data['app_id'];
//        $saveOppositeLog['type'] = 'transfer';
//        $saveOppositeLog['integral'] = $data['integral'];
//        $saveOppositeLog['remark'] = isset($userInfo['name']) ? '用户[' . $userInfo['name'] . ']转账给我积分' : '用户[' . $data['user_id'] . ']转账给我积分';
//        $saveOppositeLog['create_ip'] = $data['update_ip'];
//        $saveOppositeLog['update_ip'] = $data['update_ip'];
//        $saveOppositeLog['status'] = 1;
//        $saveOppositeLog['user_info'] = $data['user_info'];
//        $saveOppositeLog['opposite_info'] = $data['opposite_info'];
//
//        // 启动事务
//        $this->db()->startTrans();
//        try{
//            // 检查账户 a的可用积分是否充足
//            $findInfoUserAccount = $this->model($this->integralAccountModel)->lock(true)->where($whereUpdateUserAccount)->find();
//            if(is_null($findInfoUserAccount)){ return \verify_err(null, '转出用户不存在或可用积分不足'); }
//            // 扣减a 帐户可用积分
//            $resUpdateUserAccount = $this->model($this->integralAccountModel)->where($whereUpdateUserAccount)->update($updateUserAccount);
//            // 增加b 帐户可用积分
//            $resUpdateOppositeAccount = $this->model($this->integralAccountModel)->where($whereUpdateOppositeAccount)->update($updateOppositeAccount);
//            // 查询当前用户的积分状态
//            $infoUserAccount = $this->model($this->integralAccountModel)->lock(true)->where(['user_id'=>$data['user_id']])->find();
//            // 查询当前收款账户的积分状态
//            $infoOppositeAccount = $this->model($this->integralAccountModel)->lock(true)->where($whereUpdateOppositeAccount)->find();
//            if(is_null($infoUserAccount) || is_null($infoOppositeAccount)){
//                return verify_err(null, '当前用户或收款用户的积分账户不存在');
//            }
//            // 补全用户积分日志
//            $saveUserLog = $this->_replenishIntegralLogInfo($saveUserLog, $infoUserAccount);
//            // 补opposite积分日志
//            $saveOppositeLog = $this->_replenishIntegralLogInfo($saveOppositeLog, $infoOppositeAccount);
//            // 写入积分日志
//            $this->_saveIntegralLogInfo($saveOppositeLog, $saveUserLog);
//
//            // 提交事务
//            $this->db()->commit();
//        } catch (\Exception $e) {
//            // 回滚事务
//            $this->db()->rollback();
//            return server_err(null, '数据库异常:' . $e->getMessage());
//        }
//        return auto_return($infoUserAccount);
//    }

    # 冻结 a
    public function freeze(array $data): array
    {
        // 检测用户及可用积分是否正常
        $whereFindUser = [['user_id','=',$data['user_id']],['available_integral','>=',$data['integral']]];
        // 冻结相应积分
        $updateUserAccount['update_ip'] = $data['update_ip'];
        $updateUserAccount['available_integral'] = ['exp', 'available_integral-' . $data['integral']];
        $updateUserAccount['freeze'] = ['exp', 'freeze+' . $data['integral']];
        // 用户积分日志(部分)
        $userInfo = json_decode($data['user_info'], true);
        $saveUserLog['user_id'] = $data['user_id'];
        $saveUserLog['opposite_id'] = $data['opposite_id'];
        $saveUserLog['app_id'] = $data['app_id'];
        $saveUserLog['type'] = 'freeze';
        $saveUserLog['integral'] = -$data['integral'];
        $saveUserLog['remark'] = isset($userInfo['name']) ? '冻结用户' . $userInfo['name'] . '积分' : '冻结用户' . $data['user_id'] . '积分';
        $saveUserLog['create_ip'] = $data['update_ip'];
        $saveUserLog['update_ip'] = $data['update_ip'];
        $saveUserLog['status'] = 1;
        $saveUserLog['user_info'] = $data['user_info'];
        $saveUserLog['opposite_info'] = $data['opposite_info'];
        // 写入冻结记录表
        $freezeLog['user_id'] = $data['user_id'];
        $freezeLog['opposite_id'] = $data['opposite_id'];
        $freezeLog['app_id'] = $data['app_id'];
        $freezeLog['integral'] = $data['integral'];
        $freezeLog['unfreeze_integral'] = 0;
        $freezeLog['create_ip'] = $data['update_ip'];
        $freezeLog['update_ip'] = $data['update_ip'];
        $freezeLog['freeze_status'] = 0;
        $freezeLog['user_info'] = $data['user_info'];
        $freezeLog['opposite_info'] = $data['opposite_info'];
        $freezeLog['type'] = $data['type'];
        $freezeLog['obj_id'] = $data['obj_id'];
        $freezeLog['freeze_counts'] = $data['freeze_counts'];
        // 启动事务
        DB::beginTransaction();
        try{
            // 检测用户及可用积分是否正常
            $resFind = IntegralAccount::sharedLock()->where($whereFindUser)->first();
            if(!empty($resFind)){
                $resFind = $resFind->toArray();
            }
            if(is_null($resFind)){ return \verify_err(null, '没有符合条件的用户或用户可用积分不足'); }
            // 冻结相应积分
            $resFreeze = DB::update("update `tp_integral_account` set `update_ip` = ?, `available_integral` = (available_integral-?), `freeze` = (freeze+?) where `user_id` = ?",[$data['update_ip'],$data['integral'],$data['integral'],$data['user_id']]);
            // 查询当前用户的积分状态
            $infoUserAccount = IntegralAccount::sharedLock()->where(['user_id'=>$data['user_id']])->first();
            // 补全用户积分日志
            $saveUserLog = $this->_replenishIntegralLogInfo($saveUserLog, $infoUserAccount);
            $freezeLog['integral_log_id'] = $this->_saveIntegralLogInfo($saveUserLog);
            // 写入冻结记录表
            $freezeLog['create_time'] = NOW;
            $freezeLog['update_time'] = NOW;
            IntegralFreezeLog::insert($freezeLog);
            // 提交事务
            DB::commit();
        } catch (\Exception $e) {
            // 回滚事务
            DB::rollback();
            return server_err(null, '数据库异常:' . $e->getMessage());
        }
        return auto_return($data);
    }

    /*管理员冻结用户指定积分*/
    public function adminFreeze($data): array
    {
        //添加一条管理员操作原因记录表
        $data['create_ip'] = $data['update_ip'];
        $data['type'] = 'freeze_integral';
        $data['money'] = $data['integral'];
        $data['update_time'] = NOW;
        $data['create_time'] = NOW;
        $data1 = array_except($data, ['token','integral']);
        $res = OperateReason::insertGetId($data1);
        if(!$res){
            return verify_err(null, '添加操作原因记录失败');
        }
        $data['type'] = 'system';
        $data['obj_id'] = $res;
        $data['freeze_counts'] = 1;
        return $this->freeze($data);
    }

    # 解冻 a
    public function unfreeze(array $data): array
    {
        // 检测是否存在冻结日志及积分是否符合标准
        $whereFindUser = [['user_id', '=', $data['user_id']],['freeze', '>=', $data['integral']]];
        // 检测冻结日志表
        $whereFreezeLog = [['type','=',$data['type']],['obj_id','=',$data['obj_id']],['freeze_counts','=',$data['freeze_counts']],['user_id','=',$data['user_id']],['status','=',1],['freeze_status','=',0]];
        // 用户积分日志(部分)
        $updateUserAccount['update_ip'] = $data['update_ip'];
        $updateUserAccount['available_integral'] = ['exp', 'available_integral+' . $data['integral']];
        $updateUserAccount['freeze'] = ['exp', 'freeze-' . $data['integral']];
        // 用户积分日志(部分)
        $userInfo = json_decode($data['user_info'], true);
        $saveUserLog['user_id'] = $data['user_id'];
        $saveUserLog['opposite_id'] = $data['opposite_id'];
        $saveUserLog['app_id'] = $data['app_id'];
        $saveUserLog['type'] = 'unfreeze';
        $saveUserLog['integral'] = $data['integral'];
        $saveUserLog['remark'] = isset($userInfo['name']) ? '解冻用户' . $userInfo['name'] . '积分' : '解冻用户' . $data['user_id'] . '积分';
        $saveUserLog['create_ip'] = $data['update_ip'];
        $saveUserLog['update_ip'] = $data['update_ip'];
        $saveUserLog['status'] = 1;
        $saveUserLog['user_info'] = $data['user_info'];
        $saveUserLog['opposite_info'] = $data['opposite_info'];
        // 写入解冻记录表
        $unfreezeLog['user_id'] = $data['user_id'];
        $unfreezeLog['opposite_id'] = $data['opposite_id'];
        $unfreezeLog['app_id'] = $data['app_id'];
        $unfreezeLog['integral'] = $data['integral'];
        /*$unfreezeLog['freeze_log_id'] = $data['freeze_log_id'];*/
        $unfreezeLog['create_ip'] = $data['update_ip'];
        $unfreezeLog['update_ip'] = $data['update_ip'];
        $unfreezeLog['status'] = 1;
        $unfreezeLog['user_info'] = $data['user_info'];
        $unfreezeLog['opposite_info'] = $data['opposite_info'];
        $unfreezeLog['remark'] = $data['remark'] ?? '';

        // 将已全部解冻的记录表进行标记
        // 启动事务
        DB::beginTransaction();
        try{
            // 检测用户及可用积分是否正常
            $resFind = IntegralAccount::sharedLock()->where($whereFindUser)->first();
            if(!empty($resFind)){
                $resFind = $resFind->toArray();
            }
            if(is_null($resFind)){ return \verify_err(null, '没有符合条件的用户或用户已冻结积分不足'); }
            // 检测冻结日志表
            $resFindFreezeLog = IntegralFreezeLog::where($whereFreezeLog)->first();
            if(is_null($resFindFreezeLog)){ return \verify_err(null, '没有找到相应的积分冻结记录'); }
            // 冻结相应积分
            DB::update("update `tp_integral_account` set `update_ip` = ?, `available_integral` = (available_integral+?), `freeze` = (freeze-?) where `user_id` = ?",[$data['update_ip'],$data['integral'],$data['integral'],$data['user_id']]);
            // 查询当前用户的积分状态
            $infoUserAccount = IntegralAccount::sharedLock()->where(['user_id'=>$data['user_id']])->first();
            if($infoUserAccount['freeze'] < 0) { return \verify_err($infoUserAccount['freeze'], '解冻后的冻结积分小于0'); }
            // 补全用户积分日志
            $saveUserLog = $this->_replenishIntegralLogInfo($saveUserLog, $infoUserAccount);

            $unfreezeLog['integral_log_id'] = $this->_saveIntegralLogInfo($saveUserLog);
            $unfreezeLog['freeze_log_id'] = $resFindFreezeLog['id'];
            // 写入解冻记录表
            $unfreezeLog['create_time'] = NOW;
            $unfreezeLog['update_time'] = NOW;
            IntegralUnfreezeLog::insert($unfreezeLog);
            // 更改冻结记录表
            DB::update("update `tp_integral_freeze_log` set `unfreeze_integral` = (unfreeze_integral+?) where `id` = ?",[$data['integral'],$resFindFreezeLog['id']]);
            // 将已全部解冻的记录表进行标记
            $this->_autoSetFreezeLogStatus(['freeze_log_id'=>$resFindFreezeLog['id'],'integral'=>$data['integral']]);
            // 提交事务
           DB::commit();
        } catch (\Exception $e) {
            // 回滚事务
            DB::rollback();
            return server_err(null, '数据库异常:' . $e->getMessage());
        }
        return auto_success($data);
    }

    // 设置已全部解冻的冻结日志表的状态
    private function _autoSetFreezeLogStatus(array $data): array
    {
        $where = [['id', '=', $data['freeze_log_id']],['unfreeze_integral', '>=', $data['integral']]];
        $res = IntegralFreezeLog::where($where)->update(['freeze_status'=>'1']);
        return auto_return($res);
    }

    // 用户积分列表
    public function list(array $data): array
    {
        $where['status'] = 1;
        $data['keywords'] && $where['user_id'] = ['like', '%' . $data['keywords'] . '%'];
        $query = IntegralAccount::where($where);
        if($data['keywords']){
            $query->where('user_id', 'like',  '%' . $data['keywords'] . '%');
        }
        $res = $query->orderBy('id', 'desc')
            ->paginate($data['list_rows'], null, null , $data['page'])
            ->toArray();
        $res['data'] = $this->_addLotsUserInfo($res['data']);
        return auto_return_page($res, '已找到用户积分列表数据', '没有用户积分列表数据');
    }

    // 某user_ids的用户积分列表
    public function listByUserIds(array $data): array
    {
        $where['status'] = 1;
       // $where['user_id'] = ['IN', $data['user_ids']];
        $user_ids = explode(",", $data['user_ids']);
        $res = IntegralAccount::where($where)->whereIn('user_id', $user_ids)->get();
        $this->_addLotsUserInfo($res);
        return auto_return($res, '已找到用户积分列表数据', '没有用户积分列表数据');
    }

    private function _addLotsUserInfo($data){
        $ids = [];
        foreach($data as $k=>$v){
            $ids[] = $v['user_id'];
        }
        $ids = implode(",",$ids);
        $arrayData = [];
        $userInfo = (new User())->infoByIds(['ids'=>$ids]);
        foreach($userInfo['res'] as $k=>$v){
            $arrayData[] = array_add($data[$k], 'user_info', $v);
        }
//        $arrayData = [];
//        foreach($data as $k=>$v){
//            $userInfo = (new User())->infoByIds(['ids'=>$v['user_id']]);
//            $arrayData[] = array_add($v, 'user_info', $userInfo['res'][0]);
//        }
        return $arrayData;
    }

//    /*获取某订单实际能抵扣的积分信息以及支付信息  转换积分时，进行向上取整,且不再转回去*/
//    //最终积分抵扣值：//用户剩余积分个数a   订单中最大抵扣积分个数b   总金额（人民币/亚数币）对应的积分个数c
//    public function getIntegralDeductionInfo($data): array
//    {
//        $res = [];
//        $classPath = '\app\serverfund\logic' . '\\' . 'FundAccount';
//        //总额（人民币/亚数币）进行兑换积分
//        if($data['fund_type'] == 'money'){
//            $exchange['app_id'] = $data['app_id'];
//            $exchange['exchange'] = 'Fund';
//            $exchange['money'] = $data['order_total_price'];
//            $Obj = new $classPath($exchange);
//            $exchangeRes = $this->exchange($Obj, $exchange);   //人民币总额所兑换的积分 c
//        }else{
//            $exchange['app_id'] = $data['app_id'];
//            $exchange['exchange'] = 'Currency';
//            $exchange['money'] = $data['order_currency_total_price'];
//            $exchange['currency_scale'] = $data['currency_scale'];
//            $Obj = new $classPath($exchange);
//            $exchangeRes = $this->exchange($Obj, $exchange);   //亚数币总额所兑换的积分 c
//        }
//        $arr = [];
//        $arr[] = $data['user_available_integral'];
//        $arr[] = $data['order_max_integral_deduction'];
//        $arr[] = $exchangeRes['res']['autoRes'];
//        //取$a $b $c 的最小值
//        sort($arr);
//        $min = $arr[0];
//        if($min == $exchangeRes['res']['autoRes']){  //全部用积分抵扣 积分向上取整 平台不吃亏
//            $min = ceil($min);
//            $res['integral_deduction'] = $min;
//            $res['integral_deduction_fund'] = $data['fund_type'] == 'money' ? $data['order_total_price'] : $data['order_currency_total_price'];
//            //应付资金
//            $res['total_price'] = $data['fund_type'] == 'money' ? $data['order_total_price'] : $data['order_currency_total_price'];
//            $res['final_total_price'] = 0; //实付资金
//            return auto_return($res);
//        }
//        if($min == 0){  //不使用积分抵扣
//            $res['integral_deduction'] = 0;
//            $res['integral_deduction_fund'] = 0;
//            //应付资金
//            $res['total_price'] = $data['fund_type'] == 'money' ? $data['order_total_price'] : $data['order_currency_total_price'];
//            $res['final_total_price'] = $res['total_price']; //实付资金
//            return auto_return($res);
//        }
//        //获取实际抵扣积分个数 $min 所兑换的人民币或亚数币的值
//        $exchangeFund['app_id'] = $data['app_id'];
//        $exchangeFund['exchange'] = 'Integral';
//        $exchangeFund['money'] = $min;
//        $exchangeFund['currency_scale'] = $data['currency_scale'];
//        if($data['fund_type'] == 'money'){    //获取$min个积分兑换的人民币的值
//            $exchangeClassPath = '\app\serverfund\logic' . '\\' . 'IntegralAccount';
//            $ExchangeObj = new $exchangeClassPath($exchangeFund);
//            $exchangeFundRes = logic('\app\serverfund\logic\FundAccount')->exchange($ExchangeObj, $exchangeFund);
//        }else{                          //获取$min个积分兑换的亚数币的值
//            $exchangeClassPath = '\app\serverfund\logic' . '\\' . 'FundAccount';
//            $ExchangeObj = new $exchangeClassPath($exchangeFund);
//            $exchangeFundRes = logic('\app\serverfund\logic\CurrencyAccount')->exchange($ExchangeObj, $exchangeFund);
//        }
//        if($exchangeFundRes['status'] != SUCCESS_STATUS){
//            return $exchangeFundRes;
//        }
//        $res['integral_deduction'] = $min;
//        $res['integral_deduction_fund'] = $data['fund_type'] == 'money' ? floor($exchangeFundRes['res']['autoRes']) : round($exchangeFundRes['res']['autoRes'], 4);   //转换人民币或亚数币向下取整
//        //应付资金
//        $res['total_price'] = $data['fund_type'] == 'money' ? $data['order_total_price'] : $data['order_currency_total_price'];
//        $res['final_total_price'] = bcsub($res['total_price'], $res['integral_deduction_fund']); //实付资金
//        return auto_return($res);
//    }
//
//    // 和上一个方法同理，获取证书的支付信息
//    public function getCertiPayInfo($data): array
//    {
//        $res = [];
//        $classPath = '\app\serverfund\logic' . '\\' . 'FundAccount';
//
//        //总额（人民币）进行兑换积分
//        $exchange['app_id'] = $data['app_id'];
//        $exchange['exchange'] = 'Fund';
//        $exchange['money'] = $data['certi_total_price'];
//        $Obj = new $classPath($exchange);
//        $exchangeRes = $this->exchange($Obj, $exchange);   //人民币总额所兑换的积分 c
//
//        $arr = [];
//        $arr[] = $data['user_available_integral'];
//        $arr[] = $data['certi_max_integral_deduction'];
//        $arr[] = $exchangeRes['res']['autoRes'];
//        //取$a $b $c 的最小值
//        sort($arr);
//        $min = $arr[0];
//        if($min == $exchangeRes['res']['autoRes']){  //全部用积分抵扣 积分向上取整 平台不吃亏
//            $min = ceil($min);
//            $res['integral_deduction'] = $min;
//            $res['integral_deduction_fund'] = $data['certi_total_price'];
//            //应付资金
//            $res['total_price'] = $data['certi_total_price'];
//            $res['final_total_price'] = 0; //实付资金
//            return auto_return($res);
//        }
//        if($min == 0){  //不使用积分抵扣
//            $res['integral_deduction'] = 0;
//            $res['integral_deduction_fund'] = 0;
//            //应付资金
//            $res['total_price'] = $data['certi_total_price'];
//            $res['final_total_price'] = $res['total_price']; //实付资金
//            return auto_return($res);
//        }
//        //获取实际抵扣积分个数 $min 所兑换的人民币的值
//        $exchangeFund['app_id'] = $data['app_id'];
//        $exchangeFund['exchange'] = 'Integral';
//        $exchangeFund['money'] = $min;
//        //获取$min个积分兑换的人民币的值
//        $exchangeClassPath = '\app\serverfund\logic' . '\\' . 'IntegralAccount';
//        $ExchangeObj = new $exchangeClassPath($exchangeFund);
//        $exchangeFundRes = logic('\app\serverfund\logic\FundAccount')->exchange($ExchangeObj, $exchangeFund);
//
//        if($exchangeFundRes['status'] != SUCCESS_STATUS){
//            return $exchangeFundRes;
//        }
//        $res['integral_deduction'] = $min;
//        $res['integral_deduction_fund'] = floor($exchangeFundRes['res']['autoRes']);   //转换人民币或亚数币向下取整
//        //应付资金
//        $res['total_price'] = $data['certi_total_price'];
//        $res['final_total_price'] = bcsub($res['total_price'], $res['integral_deduction_fund']); //实付资金
//        return auto_return($res);
//    }
}